import React, { useState } from "react";
import "./index.scss";
import LoginBox from "./loginBox";
import ChangePwd from "./changePwd";
import CheckIdentity from "./checkIdentity";
import cookie from "react-cookies";
import { message } from "antd";
import { useTranslation } from "react-i18next";
import { useNavigate } from "react-router-dom";

export default function Login(props) {
  const { t } = useTranslation();
  const navigate = useNavigate();
  const [changePwdShow, setChangePwdShow] = useState(false);
  const [checkIdentityShow, setCheckIdentityShow] = useState(false);
  const [username, setUsername] = useState("");
  const [password, setPassword] = useState("");
  const [email, setEmail] = useState("");
  // 登录成功
  const login_success_func = (res) => {
    message.success({
      content: t("login.login_success"),
    });
    if (cookie.load("login_timer")) {
      cookie.remove("login_timer");
    }
    sessionStorage.setItem("session_id", res.data.session_id);
    sessionStorage.setItem("company_id", res.data.company_id);
    cookie.save("login_timer", res.data.cookie, {
      expires: new Date(new Date().getTime() + 24 * 3600 * 1000 * 180),
    });
    navigate(`/index`);
  };
  return (
    <div className="login">
      <LoginBox
        setChangePwdShow={setChangePwdShow}
        setCheckIdentityShow={setCheckIdentityShow}
        login_success_func={login_success_func}
        username={username}
        password={password}
        setUsername={setUsername}
        setPassword={setPassword}
        setEmail={setEmail}
      />
      <ChangePwd
        changePwdShow={changePwdShow}
        setChangePwdShow={setChangePwdShow}
        login_success_func={login_success_func}
        username={username}
      />
      <CheckIdentity
        checkIdentityShow={checkIdentityShow}
        email={email}
        setCheckIdentityShow={setCheckIdentityShow}
        login_success_func={login_success_func}
      />
    </div>
  );
}
